/**
 *	@author: Kathleen Go
 *	date created: January 15, 2006
 *	version: 1.0
 * modified: April 13,2006
 */

package text.learning;

import java.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;

import text.obj.TExtChunk;
import text.util.DBConnector;
import text.util.TExtLearnerUtility;

public class TExtChunkLexicon{	
	Vector vEquivTarget;
	char cTarget;
	TExtLearnerUtility tlu;
	
	public TExtChunkLexicon(){
		vEquivTarget = new Vector();
		cTarget = 'f';
		tlu = new TExtLearnerUtility(false);
	}
	
	public TExtChunkLexicon(char cTarget){
		vEquivTarget = new Vector(1);
		this.cTarget = cTarget;
		if(cTarget == 'f')
			tlu = new TExtLearnerUtility(false);
		else
			tlu = new TExtLearnerUtility(true);
	}
	
	/**	Returns a vector of equivalent chunks in the Target.
	 */
	public Vector lookup(String strChunk, String strCompID){
		String strQuery;
		String strTargetField;
		
		if(cTarget == 'f'){
			strTargetField = "chunkFilipino";
			strQuery = "Select * from Chunks where chunkEnglish = '" + strChunk.replace("'", "''") + "' AND compIDEnglish = '" + strCompID + "'";
		}
		else{
			strTargetField = "chunkEnglish";
			strQuery = "Select * from Chunks where chunkFilipino = '" + strChunk.replace("'", "''") + "' AND compIDFilipino = '" + strCompID + "'";
		}	
		return retrieveChunk(strQuery, strTargetField, null);
	}
	
	/**	Returns chunk pairs such that the Source chunk contains strChunk as a token.
	 */
	public Vector lookup(String strChunk){
		String strQuery;
		String strSourceField;
		String strTargetField;
		
		if(cTarget == 'f'){
			strSourceField = "chunkEnglish";
			strTargetField = "chunkFilipino";
			strQuery = "Select * from Chunks where chunkEnglish LIKE '" + strChunk + "'";
		}
		else{
			strSourceField = "chunkFilipino";
			strTargetField = "chunkEnglish";
			strQuery = "Select * from Chunks where chunkFilipino LIKE '" + strChunk + "'";
		}
		return retrieveChunk(strQuery, strTargetField, strSourceField);
	}
	
	/**	Performs the actual DB lookup.
	 *	Returns the Vector of results.
	 */
	public Vector retrieveChunk(String strQuery, String strTargetField, String strSourceField){
		ResultSet rsEquivWords;
		String strCompIDFieldT;
		String strCompIDFieldS = null;
		
		rsEquivWords = DBConnector.select(strQuery);
		
		vEquivTarget.clear();
		
		if(strTargetField.equals("chunkFilipino")){
			strCompIDFieldT = "compIDFilipino";
			if(strSourceField != null)
				strCompIDFieldS = "compIDEnglish";
		}
		else{
			strCompIDFieldT = "compIDEnglish";
			if(strSourceField != null)
				strCompIDFieldS = "compIDFilipino";
		}
		try{
			while(rsEquivWords.next()){
				String strDomain = rsEquivWords.getString("domain");
				if(strSourceField != null)
					vEquivTarget.add(tlu.adjustChunkCompound(new TExtChunk(rsEquivWords.getString(strSourceField),
						strDomain,
						rsEquivWords.getString(strCompIDFieldS))));
				vEquivTarget.add(tlu.adjustChunkCompound(new TExtChunk(rsEquivWords.getString(strTargetField),
					strDomain,
					rsEquivWords.getString(strCompIDFieldT))));
			}
			rsEquivWords.close();
			
		}	
		catch(SQLException e){}

		rsEquivWords = null;
		if(vEquivTarget.size() == 0)
			return null;
		return vEquivTarget;
	}
}